package com.dycx.api.controller.manage.user;

import com.dycx.common.RestResponse;
import com.dycx.common.annotation.WriterLog;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.user.dto.UserDTO;
import com.dycx.user.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;


@RestController
@RequestMapping(value = "/manage/user/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 添加
     **/
    @WriterLog(hideResponse = true, bizType = BizTypeEnum.USER_ACCOUNT, bizValue = "{#userDto.uid}")
    @PostMapping("/insert")
    public RestResponse<?> insert(@RequestBody @Valid UserDTO userDto) {
        return RestResponse.success(userService.insert(userDto));
    }

    /**
     * 更新
     **/
    @WriterLog(bizType = BizTypeEnum.USER_ACCOUNT, bizValue = "{#userDto.uid}")
    @PostMapping("/update")
    public RestResponse<?> update(@RequestBody UserDTO userDto) {
        return userService.update(userDto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 更新状态
     **/
    @WriterLog(bizType = BizTypeEnum.USER_ACCOUNT, bizValue = "{#userDto.uid}")
    @PostMapping("/updateStatus")
    public RestResponse<?> updateStatus(@RequestBody UserDTO userDto) {
        return userService.updateStatus(userDto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 刪除
     **/
    @WriterLog(bizType = BizTypeEnum.USER_ACCOUNT, bizValue = "{#uid}")
    @GetMapping("/delete")
    public RestResponse<?> delete(int uid) {
        return userService.delete(uid) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 重置密码
     **/
    @WriterLog(hideResponse = true, bizType = BizTypeEnum.USER_ACCOUNT, bizValue = "{#uid}")
    @GetMapping("/restPassword")
    public RestResponse<?> restPassword(int uid) {
        return RestResponse.success(userService.restPassword(uid));
    }

    /**
     * 查询 根据主键 id 查询
     **/
    @GetMapping("/getInfoByUid")
    public RestResponse<?> getInfoByUid(int uid) {
        return RestResponse.success(userService.load(uid));
    }

    /**
     * 查询 分页查询
     **/
    @PostMapping("/getPage")
    public RestResponse<?> getPage(@RequestBody UserDTO userDto) {
        return RestResponse.success(userService.getPage(userDto));
    }

}
